package cn.hua.trip.admin.controller;

import cn.hua.trip.base.domain.Travel;
import cn.hua.trip.base.domain.TravelContent;
import cn.hua.trip.base.query.TravelQueryObject;
import cn.hua.trip.base.service.ITravelService;
import cn.hua.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/travel")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    //待审核的游记
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") TravelQueryObject queryObject) {
        //设置状态为待审核状态
        queryObject.setState(Travel.STATE_WAITING);
        //是否公开设置为true
        queryObject.setIsPublic(true);
        //根据创建时间倒序
        queryObject.setOrderBy("t.createTime desc");
        model.addAttribute("pageInfo", travelService.queryForList(queryObject));
        return "/travel/list";
    }

    //已发布的游记
    @RequestMapping("/releaseList")
    public String releaseList(Model model, @ModelAttribute("qo") TravelQueryObject queryObject) {
        //设置状态为发布状态
        queryObject.setState(Travel.STATE_RELEASE);
        //是否公开设置为true
        queryObject.setIsPublic(true);
        //根据创建时间倒序
        queryObject.setOrderBy("t.releaseTime desc");
        model.addAttribute("pageInfo", travelService.queryForList(queryObject));
        return "/travel/releaseList";
    }


    @RequestMapping("/showContent")
    @ResponseBody
    public Object showContent(Long travelId) {
        TravelContent content = travelService.getContentById(travelId);
        return content;
    }

    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(Travel travel) {
        JSONResult result = new JSONResult();
        try {
            travelService.changeState(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
